import httpService from '../httpService';
import Taro from '@tarojs/taro';

/**
 * 获取全局字典
 * @returns
 */
function getAllDictionaries() {
  return httpService.get(`/common/get_all_dictionaries`);
}

/**
 * 根据类型获取问卷
 * @param  type type 问卷类型 1 个人 2机构 3 冷静期 4 适当性 5 风险讲解问卷
 * @returns
 */
function getQuestionnaire(type) {
  return httpService.get(`/compli/get/questionnaire_type`, { 'type': type });
}




/**
 * 查询风险接受等级
 * @param  type type 问卷类型 1 个人 2机构 3 冷静期 4 适当性 5 风险讲解问卷
 * @returns
 */
function queryRiskSection() {
  return httpService.get(`/compli/query/risk_section`);
}


/**
 * 请求问题
 * @param  type type 问卷类型 1 个人 2机构 3 冷静期 4 适当性 5 风险讲解问卷
 * @returns
 */
function askQuestion(prompt, questionId) {
  var params = { 'prompt': prompt, 'questionId': questionId, 'isShowLoading': false }
  return httpService.post(`/gpt/question`, params, false);
}


/**
 * 获取问题答案
 * @param  requestId 请求id
 * @returns
 */
function getQuestionAnswer(requestId) {
  var params = { 'requestId': requestId, 'isShowLoading': false }
  return httpService.get(`/gpt/get/answer`, params, false,);
}



export {
  getAllDictionaries,
  getQuestionnaire,
  queryRiskSection,
  askQuestion,
  getQuestionAnswer
};








/**
 * 获取字典里面的数据
 * @param params
 * @returns
 */
export function findMapsByFieldNameAndDictItemValue(dictFieldName, dictItemValue) {
  if (dictItemValue === undefined || dictItemValue === null) {
    return {}
  }
  const globalDict = Taro.getStorageSync('globalDict');

  console.log(dictFieldName);

  const foundMap = globalDict.find((item) => {
    return item.dictItemValue === dictItemValue.toString() && item.dictFieldName === dictFieldName;
  });
  console.log(foundMap);

  return foundMap;
}


/**
 * 获取字典里面的数据
 * @param params
 * @returns
 */
export function findMapsByFieldNameAndDictItemName(dictFieldName) {
  if (dictFieldName === undefined) {
    return {}
  }
  const globalDict = Taro.getStorageSync('globalDict');
  const foundMap = globalDict.filter((item) => {
    return item.dictFieldName === dictFieldName;
  });
  return foundMap;
}

/**
 * 获取字典里面的数据
 * @param params
 * @returns
 */
export function findNameByFieldNameAndDictItemValue(dictFieldName, dictItemValue) {
  if (dictItemValue === undefined || dictItemValue === null) {
    return ''
  }
  const globalDict = Taro.getStorageSync('globalDict');
  const foundMap = globalDict.find((item) => {
    return item.dictItemValue === dictItemValue.toString() && item.dictFieldName === dictFieldName;
  });
  // console.log(foundMap)
  if (foundMap !== undefined) {
    return foundMap['dictItemName'];
  }
  return '';
}

//
/**
 * 根据 dictFieldName 查找符合条件的多个项
 * @param dictFieldName
 * @returns
 */
export function findMapsByFieldName(dictFieldName) {
  const globalDict = Taro.getStorageSync('globalDict');
  const foundMaps = globalDict.filter((item) => {
    return item.dictFieldName === dictFieldName;
  });
  return foundMaps;
}


/**
 * 保存字典里面的数据
 * @param data
 */
export function saveGlobalDict(data) {
  Taro.setStorageSync('globalDict', data);
}
